package com.bindatax.event.impl.statistics.change;

import com.bindatax.core.statistics.ExecuteStatistics;
import com.bindatax.core.statistics.StatisticsData;
import com.bindatax.core.task.DataTask;
import com.bindatax.core.task.event.BaseListenEvent;
import com.bindatax.core.task.event.EventType;
import com.bindatax.core.task.event.ListenEventInterface;
import com.bindatax.core.task.row.EventValueData;
import com.bindatax.core.task.segment.SegmentWork;

import java.util.HashSet;

public class TaskEventStatisticsChange extends BaseListenEvent implements ListenEventInterface {
    @Override
    public int getOrder() {
        return 0;
    }

    @Override
    public HashSet<EventType> getTypes() {
        eventTypes.add(EventType.TASK_STATISTICS_CHANGE);
        return eventTypes;
    }

    @Override
    public void exe(EventType type, DataTask thisTask, SegmentWork thisSegmentWork, EventValueData rowData) {
        if(EventType.TASK_STATISTICS_CHANGE == type){
            StatisticsData statisticsData = thisTask.getTaskStatisticsData();
            ExecuteStatistics previousTaskStatistics = statisticsData.getPreviousTaskStatistics();//上一次的统计值
            ExecuteStatistics taskStatistics = statisticsData.getTaskStatistics();//实时统计值
            String msg = String.format("数据执行统计变化 - 耗时：%s毫秒 - 读：%s条[总：%s条] / 写：%s条[总：%s条] / 输入：%s条[总：%s条] / 输出：%s条[总：%s条] / 更新：%s条[总：%s条] / 拒绝：%s条[总：%s条] / 错误：%s条[总：%s条]",
                    taskStatistics.getTime() - previousTaskStatistics.getTime(),
                    taskStatistics.getRead() - previousTaskStatistics.getRead(),
                    taskStatistics.getRead(),
                    taskStatistics.getWrite() - previousTaskStatistics.getWrite(),
                    taskStatistics.getWrite(),
                    taskStatistics.getInput() - previousTaskStatistics.getInput(),
                    taskStatistics.getInput(),
                    taskStatistics.getOutput() - previousTaskStatistics.getOutput(),
                    taskStatistics.getOutput(),
                    taskStatistics.getUpdate() - previousTaskStatistics.getUpdate(),
                    taskStatistics.getUpdate(),
                    taskStatistics.getReject() - previousTaskStatistics.getReject(),
                    taskStatistics.getReject(),
                    taskStatistics.getError() - previousTaskStatistics.getError(),
                    taskStatistics.getError()
                    );
            LOG.info(msg);
        }
    }

}
